# Create your views here.

from django.db.models import Q
from django.shortcuts import render_to_response
from models import Publisher
from forms import ContactForm

def search(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(name__icontains=query) |
            Q(address__icontains=query) |
            Q(city__icontains=query) |
            Q(state_province__icontains=query) |
            Q(country__icontains=query) |
            Q(website__icontains=query) 
        )
        results = Publisher.objects.filter(qset).distinct()
    else:
        results = []
    return render_to_response("books/search.html", {
        "results": results,
        "query": query
    })

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
    else:
        form = ContactForm()
    return render_to_response('books/contact.html', {'form':form})

